import os
from PyQt6.QtWidgets import QApplication

class StyleManager:
    """样式管理器"""
    
    @staticmethod
    def load_qss_file(file_path):
        """加载QSS文件"""
        try:
            if os.path.exists(file_path):
                with open(file_path, 'r', encoding='utf-8') as f:
                    return f.read()
            else:
                print(f"QSS文件不存在: {file_path}")
                return ""
        except Exception as e:
            print(f"加载QSS文件失败: {e}")
            return ""
    
    @staticmethod
    def apply_app_style(app):
        """应用应用程序全局样式"""
        # 获取当前文件所在目录
        current_dir = os.path.dirname(os.path.abspath(__file__))
        # 构建QSS文件路径
        qss_file_path = os.path.join(current_dir, '..', 'styles', 'app.qss')
        
        # 加载QSS样式
        qss_content = StyleManager.load_qss_file(qss_file_path)
        
        if qss_content:
            # 应用样式到应用程序
            app.setStyleSheet(qss_content)
            print("应用程序样式已应用")
        else:
            print("应用程序样式应用失败")
    
    @staticmethod
    def apply_style_to_widget(widget, style_file):
        """应用指定样式文件到控件"""
        # 获取当前文件所在目录
        current_dir = os.path.dirname(os.path.abspath(__file__))
        # 构建QSS文件路径
        qss_file_path = os.path.join(current_dir, '..', 'styles', style_file)
        
        # 加载QSS样式
        qss_content = StyleManager.load_qss_file(qss_file_path)
        
        if qss_content:
            # 应用样式到指定控件
            widget.setStyleSheet(qss_content)
            print(f"样式已应用到控件: {style_file}")
        else:
            print(f"样式应用失败: {style_file}")
    
    @staticmethod
    def set_button_style(button, style_class=""):
        """设置按钮样式类"""
        if style_class:
            button.setProperty("class", style_class)
            button.style().unpolish(button)
            button.style().polish(button)
    
    @staticmethod
    def set_label_style(label, style_class=""):
        """设置标签样式类"""
        if style_class:
            label.setProperty("class", style_class)
            label.style().unpolish(label)
            label.style().polish(label)
    
    @staticmethod
    def set_frame_style(frame, style_class=""):
        """设置框架样式类"""
        if style_class:
            frame.setProperty("class", style_class)
            frame.style().unpolish(frame)
            frame.style().polish(frame)
    
    @staticmethod
    def set_list_widget_style(list_widget, style_class=""):
        """设置列表控件样式类"""
        if style_class:
            list_widget.setProperty("class", style_class)
            list_widget.style().unpolish(list_widget)
            list_widget.style().polish(list_widget)
    
    @staticmethod
    def set_table_widget_style(table_widget, style_class=""):
        """设置表格控件样式类"""
        if style_class:
            table_widget.setProperty("class", style_class)
            table_widget.style().unpolish(table_widget)
            table_widget.style().polish(table_widget)
    
    @staticmethod
    def set_splitter_style(splitter, style_class=""):
        """设置分割器样式类"""
        if style_class:
            splitter.setProperty("class", style_class)
            splitter.style().unpolish(splitter)
            splitter.style().polish(splitter)
    
    @staticmethod
    def set_widget_style(widget, style_class=""):
        """通用设置控件样式类"""
        if style_class:
            widget.setProperty("class", style_class)
            widget.style().unpolish(widget)
            widget.style().polish(widget)
    
    @staticmethod
    def set_action_button_style(button, action_type="", color="#007bff"):
        """设置操作按钮样式"""
        button.setProperty("class", "action-btn")
        # 设置CSS变量
        button.setStyleSheet(f"""
            QPushButton {{
                --action-color: {color};
                --action-hover-color: {StyleManager._darken_color(color)};
            }}
        """)
        button.style().unpolish(button)
        button.style().polish(button)
    
    @staticmethod
    def _darken_color(color, factor=0.8):
        """将颜色变暗"""
        # 简单的颜色变暗处理
        if color.startswith("#"):
            # 移除#号
            color = color[1:]
            # 转换为RGB
            r = int(color[0:2], 16)
            g = int(color[2:4], 16)
            b = int(color[4:6], 16)
            # 变暗
            r = int(r * factor)
            g = int(g * factor)
            b = int(b * factor)
            # 转回十六进制
            return f"#{r:02x}{g:02x}{b:02x}"
        return color
    
    @staticmethod
    def create_button_with_style(text, style_class="", parent=None):
        """创建带样式的按钮"""
        from PyQt6.QtWidgets import QPushButton
        button = QPushButton(text, parent)
        if style_class:
            StyleManager.set_button_style(button, style_class)
        return button
    
    @staticmethod
    def create_label_with_style(text, style_class="", parent=None):
        """创建带样式的标签"""
        from PyQt6.QtWidgets import QLabel
        label = QLabel(text, parent)
        if style_class:
            StyleManager.set_label_style(label, style_class)
        return label
    
    @staticmethod
    def create_frame_with_style(style_class="", parent=None):
        """创建带样式的框架"""
        from PyQt6.QtWidgets import QFrame
        frame = QFrame(parent)
        if style_class:
            StyleManager.set_frame_style(frame, style_class)
        return frame
    
    @staticmethod
    def create_list_widget_with_style(style_class="", parent=None):
        """创建带样式的列表控件"""
        from PyQt6.QtWidgets import QListWidget
        list_widget = QListWidget(parent)
        if style_class:
            StyleManager.set_list_widget_style(list_widget, style_class)
        return list_widget
    
    @staticmethod
    def create_table_widget_with_style(style_class="", parent=None):
        """创建带样式的表格控件"""
        from PyQt6.QtWidgets import QTableWidget
        table_widget = QTableWidget(parent)
        if style_class:
            StyleManager.set_table_widget_style(table_widget, style_class)
        return table_widget 